package scenery;

import com.crunch.math.Vector4f;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class BackgroundLayer {
	public BackgroundLayer() {
		depth = 0;
		offsetX = 0;
		offsetY = 0;
		parallaxX = 0;
		parallaxY = 0;
		color = new Vector4f(1.0f, 1.0f, 1.0f, 1.0f);
	}

	public JSONObject save()
			throws JSONException {
		JSONObject rootObj = new JSONObject();

		JSONObject propertiesObj = new JSONObject();
		rootObj.put("properties", propertiesObj);
		propertiesObj.put("depth", getDepth());
		propertiesObj.put("offset", new JSONArray().put(getOffsetX()).put(getOffsetY()));
		propertiesObj.put("parallax", new JSONArray().put(getParallaxX()).put(getParallaxY()));
		propertiesObj.put("color", new JSONArray().put(color.x()).put(color.y()).put(color.z()).put(color.w()));

		return rootObj;
	}

	public void load(JSONObject rootObj)
			throws IOException {
		JSONObject propertiesObj = rootObj.getJSONObject("properties");
		setDepth(propertiesObj.getInt("depth"));
		JSONArray offset = propertiesObj.getJSONArray("offset");
		setOffset(
				offset.getInt(0),
				offset.getInt(1));
		JSONArray parallax = propertiesObj.getJSONArray("parallax");
		setParallax(
				(float) parallax.getDouble(0),
				(float) parallax.getDouble(1));
		JSONArray color = propertiesObj.getJSONArray("color");
		setColor(new Vector4f(
				(float) color.getDouble(0),
				(float) color.getDouble(1),
				(float) color.getDouble(2),
				(float) color.getDouble(3)));
	}

	public int getDepth() {
		return depth;
	}

	public void setDepth(int depth) {
		this.depth = depth;
	}

	public int getOffsetX() {
		return offsetX;
	}

	public int getOffsetY() {
		return offsetY;
	}

	public void setOffset(int offsetX, int offsetY) {
		this.offsetX = offsetX;
		this.offsetY = offsetY;
	}

	public float getParallaxX() {
		return parallaxX;
	}

	public float getParallaxY() {
		return parallaxY;
	}

	public void setParallax(float parallaxX, float parallaxY) {
		this.parallaxX = parallaxX;
		this.parallaxY = parallaxY;
	}

	public Vector4f getColor() {
		return new Vector4f(color);
	}

	public void setColor(Vector4f color) {
		this.color.set(color);
	}

	private int depth;
	private int offsetX, offsetY;
	private float parallaxX;
	private float parallaxY;
	private Vector4f color;
}
